package club.justwill.single.Controller;

import club.justwill.single.Service.*;
import club.justwill.single.aspect.LoginRequired;
import club.justwill.single.bean.*;
import club.justwill.single.impl.ToolsImpl;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.model.UserType;
import club.justwill.single.persistence.jpo.ProjectIntroduce;
import club.justwill.single.persistence.jpo.User;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by jaylon on 16-1-6.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class AdminController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private ProjectIntroduceService projectIntroduceService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProjectsService projectsService;

    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = URL_USER_ADMIN_LOGIN, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse authenticate(@RequestBody UserLogin userLogin) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token =new SecurityToken(sessionState.getUserId());
        User user = userService.authenticate(token, sessionState, userLogin, obj);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
            return obj;
        } else {
            if(user != null && !JsonResponseHelper.isHasError(obj)) {
                if(user.getType() != UserType.ADMINISTRATOR){
                    JsonResponseHelper.addError(obj,CoreConstants.NOT_IS_ADMIN);
                }else{
                    UserResp userResp = new UserResp();
                    userResp.setUserId(String.valueOf(user.getUserId()));
                    obj.setData(userResp);
                    obj.setSuccessful();
                }
            }
            return obj;
        }
    }

    @LoginRequired
    @RequestMapping(value = URL_USER_UPLOAD_HEADICON, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse uploadHeadIcon(@RequestParam("head_icon") CommonsMultipartFile headIcon) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        if (headIcon == null || ToolsImpl.isEmpty(headIcon.getBytes())) {
            obj.setFailed();
        } else {
            String path = userService.uploadHeadIcon(token, obj, headIcon);
            if (JsonResponseHelper.isHasError(obj)) {
                obj.setFailed();
            } else {
                obj.setData(path);
                obj.setSuccessful();
            }
        }
        return obj;

    }

    @LoginRequired
    @RequestMapping(value = URL_ACTIVITY_COVER_UPLOAD, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse uploadActivityCover(@PathVariable("activityId") Long actiivtyId,
                                            @RequestParam("activity_cover") CommonsMultipartFile activityCover) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        if (activityCover == null || ToolsImpl.isEmpty(activityCover.getBytes())) {
            obj.setFailed();
        } else {
            activityService.uploadActivityCover(token, obj, activityCover, actiivtyId);
            if (JsonResponseHelper.isHasError(obj)) {
                obj.setFailed();
            } else {
                obj.setSuccessful();
            }
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ADMIN_PROJECT_INTRODUCE_CREATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse createProjectIntroduceCover(@PathVariable("introduceId")Long projectIntroduceId, @RequestParam("introduce_cover") CommonsMultipartFile cover){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        projectIntroduceService.saveFile(token,obj,cover,projectIntroduceId);
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_COVER_SAVE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse saveProjectFile(@PathVariable("projectId") Long projectId,
                           @RequestParam("cover") CommonsMultipartFile cover){
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        JsonResponse obj = new JsonResponse();
        if(cover != null && !ToolsImpl.isEmpty(cover.getBytes())){
            projectsService.saveFile(token,obj,cover,projectId);
        }else{
            obj.setFailed();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_SKUS_CREATE_COVER, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse saveSku(@PathVariable("skuId") Long skuId,@RequestParam("cover_skus") CommonsMultipartFile cover){
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        JsonResponse obj = new JsonResponse();
        skuService.saveSkuFile(token,obj,cover,skuId);
        obj.setSuccessful();
        return obj;
    }



}
